package com.gxa.service.impl.statistical;

import com.gxa.mapper.statistical.VisitorsMapper;
import com.gxa.pojo.entity.tjgl.*;
import com.gxa.pojo.entity.tjgl.util.DateInfo;
import com.gxa.service.statistical.VisitorsService;
import com.gxa.utils.QueryResult;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.util.List;


/**
 * @author IKE <hp1041735092@outlook.jp>
 * @date 23/2/2024
 */
@Service
public class VisitorsServiceImpl implements VisitorsService {
    @Resource
    private VisitorsMapper visitorsMapper;
    @Override
    public VisitorsOverview queryOverview() {
        VisitorsOverview overview = new VisitorsOverview();

        overview.setTodayNum(visitorsMapper.queryTodayNum());
        overview.setYestNum(visitorsMapper.queryYestNum());
        overview.setTotalNum(visitorsMapper.queryTotalNum());
        overview.setThisYearNum(visitorsMapper.queryThisYearNum());
        overview.setLastYearNum(visitorsMapper.queryLastYearNum());
        overview.setThisMonthNum(visitorsMapper.queryThisMonthNum());
        overview.setLastMonthNum(visitorsMapper.queryLastMonthNum());
        overview.setThisWeekNum(visitorsMapper.queryThisWeekNum());
        overview.setLastWeekNum(visitorsMapper.queryLastWeekNum());

        return overview;
    }

    @Override
    public QueryResult queryStaffSummary(Integer pageNum, Integer pageSize,String beginDate,String endDate,String username) {
        //convert date of String type to Timestamp and verify the data
        Timestamp startTime;
        try {
            startTime = Timestamp.valueOf(beginDate);
        }catch (Exception e) {
            startTime = Timestamp.valueOf("1970-01-01 00:00:00");
        }
        Timestamp endTime;
        try {
            endTime = Timestamp.valueOf(endDate);
        }catch (Exception e){
            endTime = null;
        }
        //convert empty username string to null
        if (!StringUtils.hasLength(username)&&username!=null){
            username = null;
        }

        //get count
        Long count = visitorsMapper.queryStaffSummaryCount();
        //get data >>> index,limit;index start from 0,limit = pageSize
        //index = (pageNum -1)*pageSize
        Integer index = (pageNum - 1) * pageSize;
        List<StaffSummary> summaryList = visitorsMapper.queryPaginatedStaffSummaryList(index,pageSize,startTime,endTime,username);


        QueryResult queryResult = new QueryResult();
        queryResult.setData(summaryList);
        queryResult.setCount(count);

        return queryResult;
    }

    @Override
    public ChannelPieChart queryPieChartReception(DateInfo date) {

        //query data based on year and month
        return visitorsMapper.queryPieChartReception(date);
    }

    @Override
    public List<VisitorsTrend> queryTrendReception(LocalDate parsedBeginDate, LocalDate parsedEndDate) {

        return visitorsMapper.queryTrendReception(parsedBeginDate,parsedEndDate);
    }

    @Override
    public VisitorsOverview queryVisitorOverview() {
        VisitorsOverview visitOverview = new VisitorsOverview();

        visitOverview.setTodayNum(visitorsMapper.queryTodayVisitNum());
        visitOverview.setYestNum(visitorsMapper.queryYestVisitNum());
        visitOverview.setTotalNum(visitorsMapper.queryTotalVisitNum());
        visitOverview.setThisYearNum(visitorsMapper.queryThisYearVisitNum());
        visitOverview.setLastYearNum(visitorsMapper.queryLastYearVisitNum());
        visitOverview.setThisMonthNum(visitorsMapper.queryThisMonthVisitNum());
        visitOverview.setLastMonthNum(visitorsMapper.queryLastMonthVisitNum());
        visitOverview.setThisWeekNum(visitorsMapper.queryThisWeekVisitNum());
        visitOverview.setLastWeekNum(visitorsMapper.queryLastWeekVisitNum());

        return visitOverview;
    }

    @Override
    public List<VisitorsTrend> queryVisitTrend(LocalDate parsedBeginDate, LocalDate parsedEndDate) {
        return visitorsMapper.queryVisitTrend(parsedBeginDate,parsedEndDate);
    }


}
